//Accepted
#include <vector>
#include <set>
#include <algorithm>
#include <string>
#include <cmath>
#include <queue>
#include <map>
#include <iostream>
#include <list>
#include <deque>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

int n, a[2001], b[2001], i, sum;

int rec(int x){
	if(b[x])return b[x];
	
	b[x]=rec(a[x])+1;
	return b[x];
}

int main (){
	//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);
	scanf("%d", &n);
	memset(b,0, sizeof(b));
	for(i=1;i<=n;++i){
		scanf("%d", &a[i]);
		if(a[i]<0)b[i]=1;
	}
	for(i=1;i<=n;++i){
		if(!b[i])b[i]=rec(i);
	}
	for(sum=0,i=1;i<=n;++i){
		if(b[i]>sum)sum=b[i];
	}
	printf("%d", sum);
	return 0;
}
